<!-- V-webapp/src/views/PaymentSuccessView.vue -->
<template>
  <div class="success-container">
    <div class="success-content">
      <!-- 成功图标 -->
      <div class="success-icon">
        <el-icon class="icon"><CircleCheck /></el-icon>
      </div>
      
      <!-- 成功信息 -->
      <div class="success-info">
        <h2 class="success-title">支付成功！</h2>
        <p class="success-desc">感谢您的购买，订单处理中</p>
        
        <div class="order-details">
          <div class="detail-item">
            <span class="label">订单号：</span>
            <span class="value">{{ orderInfo.order_number }}</span>
          </div>
          <div class="detail-item">
            <span class="label">支付金额：</span>
            <span class="value amount">¥{{ orderInfo.total_amount }}</span>
          </div>
          <div class="detail-item">
            <span class="label">支付时间：</span>
            <span class="value">{{ formatDate(orderInfo.paid_at) }}</span>
          </div>
        </div>
      </div>

      <!-- 操作按钮 -->
      <div class="action-buttons">
        <el-button 
          type="primary" 
          class="view-order-btn"
          @click="goToOrderDetail"
        >
          查看订单详情
        </el-button>
        <el-button 
          type="default" 
          class="back-home-btn"
          @click="goToHome"
        >
          返回首页
        </el-button>
      </div>

      <!-- 下一步提示 -->
      <div class="next-steps">
        <h3>下一步</h3>
        <ul>
          <li>订单将在24小时内处理</li>
          <li>您可以在"我的订单"中查看订单状态</li>
          <li>如有问题请联系客服</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { CircleCheck } from '@element-plus/icons-vue'
import { getOrderById } from '@/api/orders'

const router = useRouter()
const route = useRoute()

const orderInfo = ref({
  order_id: 0,
  order_number: '',
  total_amount: 0,
  paid_at: '',
  status: '',
  payment_status: ''
})

// 加载订单信息
const loadOrderInfo = async () => {
  try {
    const orderId = route.query.order_id as string
    if (!orderId) {
      ElMessage.error('订单信息不存在')
      router.push('/')
      return
    }

    const response = await getOrderById(parseInt(orderId))
    if (response.success) {
      orderInfo.value = response.data
    } else {
      throw new Error(response.message || '获取订单信息失败')
    }
  } catch (error: any) {
    console.error('❌ 加载订单信息失败:', error)
    ElMessage.error(error.message || '加载订单信息失败')
  }
}

// 查看订单详情
const goToOrderDetail = () => {
  router.push('/my-orders')
}

// 返回首页
const goToHome = () => {
  router.push('/')
}

// 格式化日期
const formatDate = (dateString: string) => {
  if (!dateString) return '--'
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN')
}

onMounted(() => {
  loadOrderInfo()
})
</script>

<style scoped>
.success-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  padding-bottom: 100px; /* 为底部导航留出空间 */
}

.success-content {
  background: white;
  border-radius: 20px;
  padding: 40px 30px;
  text-align: center;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  max-width: 480px; /* 限制最大宽度与手机页面一致 */
  width: 100%;
  margin: 0 auto;
}

.success-icon {
  margin-bottom: 24px;
}

.success-icon .icon {
  font-size: 80px;
  color: #52c41a;
}

.success-title {
  margin: 0 0 8px 0;
  font-size: 28px;
  font-weight: 700;
  color: #333;
}

.success-desc {
  margin: 0 0 30px 0;
  font-size: 16px;
  color: #666;
}

.order-details {
  background: #f8f9fa;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 30px;
  text-align: left;
}

.detail-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  font-size: 14px;
}

.detail-item:last-child {
  margin-bottom: 0;
}

.label {
  color: #666;
  font-weight: 500;
}

.value {
  color: #333;
  font-weight: 600;
}

.amount {
  color: #ff4757;
  font-size: 18px;
}

.action-buttons {
  display: flex;
  gap: 12px;
  margin-bottom: 30px;
}

.view-order-btn,
.back-home-btn {
  flex: 1;
  padding: 12px 20px;
  border-radius: 20px;
  font-weight: 600;
  font-size: 16px;
}

.view-order-btn {
  background: linear-gradient(135deg, #667eea, #764ba2);
  border: none;
}

.next-steps {
  text-align: left;
  border-top: 1px solid #f0f0f0;
  padding-top: 20px;
}

.next-steps h3 {
  margin: 0 0 12px 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.next-steps ul {
  margin: 0;
  padding-left: 20px;
}

.next-steps li {
  margin-bottom: 8px;
  color: #666;
  font-size: 14px;
}

.next-steps li:last-child {
  margin-bottom: 0;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .success-content {
    padding: 30px 20px;
    margin: 20px;
    max-width: 100%; /* 在小屏幕上占满宽度 */
  }
  
  .success-icon .icon {
    font-size: 60px;
  }
  
  .success-title {
    font-size: 24px;
  }
  
  .action-buttons {
    flex-direction: column;
  }
  
  .success-container {
    padding: 10px;
    padding-bottom: 80px;
  }
}
</style>
